<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 店铺促销
 * @author khj
 */
class Promotion extends MY_Controller {
	
	public function __construct(){
		parent::__construct();
	}

    /**
     * 店铺优惠信息
     */
    public function getInfo(){
        $tA = $this->mUser->getInfo($this->userId);

        $aData = array(
            'min_consume' => $tA['min_consume'],
            'first_order_reduce' => $tA['first_order_reduce'],
            'able_use_coupon' => $tA['able_use_coupon'],
        );
        $this->appReturn(1, '', $aData);
    }

    /**
     * 店铺优惠设置
     */
    public function editPromotion(){
        $this->loadModel('MerchantPromotionModel', 'mPro', 'merchant');

        //表单验证
        $vConf = array(
            array('keyword', '关键字', 'required'),
            array('value', '设置内容', 'required'),
        );
        $this->formDataValidation($vConf);
        $keyword = trim(post('keyword'));
        $value = post('value');

        if(!in_array($keyword,array('min_consume','first_order_reduce','able_use_coupon','business'))){
            $this->appReturn(0, '关键字错误');
        }

        if($keyword == 'able_use_coupon' && !in_array($value,array(1,2))){
            $this->appReturn(0, '是否使用红包错误');
        }

        if($keyword == 'business' && !in_array($value,array(1,2))){
            $this->appReturn(0, '营业状态错误');
        }

        if($keyword == 'first_order_reduce' && !is_numeric($value)){
            $this->appReturn(0, '首单减免必须是数字');
        }

        if($keyword == 'min_consume' && !is_numeric($value)){
            $this->appReturn(0, '最低消费必须是数字');
        }

        //保存信息
        if(!($this->mUser->save(array($keyword => $value), $this->userId))){
            $this->appReturn(0, '保存失败');
        }

        $this->appReturn(1, '保存成功');
    }

    /**
     * 设置代金券
     */
    public function addCashCoupon(){
        $this->loadModel('MerchantPromotionModel', 'mPro', 'merchant');

        //表单验证
        $vConf = array(
            array('money', '优惠面额', 'required|is_numeric'),
            array('valid_hour', '有效时间', 'required|is_numeric'),
            array('receive', '获得条件', 'required|is_numeric'),
            array('condition', '使用条件', 'required|is_numeric'),
            array('able_cash_coupon', '是否启用代金券', 'required|in_list[1,2]'),
        );

        $this->formDataValidation($vConf);

        $able_cash_coupon = (int)post('able_cash_coupon');

        //商家是否存在代金券
        $info = $this->mPro->getCashInfo($this->userId);

        $id = $info ? $this->userId:0;
        $money = post('money');
        if($money > 100){
            $this->appReturn(0, '优惠面额必须小于100');
        }
        $data = array(
            'merchant_id' => $this->userId,
            'money' => sprintf('%.2f', $money),
            'valid_hour' => (int)post('valid_hour'),
            'receive' => trim(post('receive')),
            'condition' => trim(post('condition')),
            'addtime' => time()
        );

        //保存信息
        $this->db->trans_begin();
        if(!($this->mPro->saveCash($data, $id))){
            $this->db->trans_rollback();
            $this->appReturn(0,'保存失败');
        }

        //保存商户信息
        if(!($this->mUser->save(array('able_cash_coupon' => $able_cash_coupon), $this->userId))){
            $this->db->trans_rollback();
            $this->appReturn(0,'保存失败');
        }

        $this->db->trans_commit();
        $this->appReturn(1,'保存成功');
    }

    /**
     * 代金券信息
     */
    public function cashCoupon(){
        $this->loadModel('MerchantPromotionModel', 'mPro', 'merchant');

        $info = $this->mPro->getCashInfo($this->userId);

        $this->appReturn(1, '', $info);
    }

    /**
     * 设置满减规则
     */
    public function addFullCut(){
        $this->loadModel('MerchantPromotionModel', 'mPro', 'merchant');

        //表单验证
        $vConf = array(
            array('start_time', '起始时间', 'required'),
            array('end_time', '结束时间', 'required')
        );

        $this->formDataValidation($vConf);

        $start_time = strtotime(post('start_time'));
        $end_time = strtotime(post('end_time'));
        $rule = trim(post('rule'));

        //商家是否存在满减活动
        $info = $this->mPro->getFullInfo($this->userId);

        $id = $info ? $this->userId:0;

        if(!is_int($start_time) || !is_int($end_time)){
            $this->appReturn(0, '时间格式错误');
        }

        if($end_time < $start_time){
            $this->appReturn(0, '结束时间必须大于开始时间');
        }

        $data = array(
            'merchant_id' => $this->userId,
            'start_time' => $start_time,
            'end_time' => $end_time + 86399,
            'addtime' => time()
        );

        $data['rule'] = '';
        $rData = array();
        if($rule){
            $ruleData  = json_decode($rule,true);

            foreach ($ruleData as $k=>$v){

                if($v['money'] > $v['max_money']){
                    $this->appReturn(0, '减免金额必须小于满额');
                }
                if($v['money'] > 100){
                    $this->appReturn(0, '减免金额必须小于100');
                }
                if($v['money'] && $v['max_money']){
                    $v['money'] = sprintf("%.2f", $v['money']);
                    $rData[] = $v;
                }
            }

            $data['rule'] = $rData? json_encode($rData):'';
        }

        //保存信息
        if(!($this->mPro->saveFull($data, $id))){
            $this->appReturn(0, '保存失败');
        }

        $this->appReturn(1, '保存成功');
    }

    /**
     * 满减信息
     */
    public function fullCut(){
        $this->loadModel('MerchantPromotionModel', 'mPro', 'merchant');

        $info = $this->mPro->getFullInfo($this->userId);

        $this->appReturn(1, '', $info);
    }



}